<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

      <div class="title-container">
        <h3 class="title logoTitle">梯口终端</h3>
      </div>

      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="用户名"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="off"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="密码"
          name="password"
          tabindex="2"
          auto-complete="new-password"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click="handleLogin">登录</el-button>

      <div class="tips">
        <span style="margin-right:20px;"><a :href="pluginUrl" class="pluginDownload">[ 下载插件 ]</a></span>
        <!--<span style="margin-right:20px;"><a href="@/assets/404_images/404.png" class="pluginDownload">[ 2下载插件 ]</a></span>-->
        <span>请使用IE11浏览器浏览网页</span>
      </div>

    </el-form>
    <object id="WebPluginActiveX2" classid="clsid:135F11E9-A923-468A-BDEF-B9AEEED4A7A6"
            codebase="WebPluginActiveX.cab#version=1,0,0,2" width="0" height="0"></object>
  </div>
</template>

<script>
  import axiosAjax from '../../utils/request'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      var reg=/^[0-9A-Za-z]{5,16}$/
      if (!reg.test(value)) {
        callback(new Error('英文 数字，5~16位'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能少于6位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      pluginUrl:'./plugin/WebOcx.exe',
      ocxObj:''
    }
  },
  mounted:function () {
    sessionStorage.clear()
    this.ocxObj=document.getElementById('WebPluginActiveX2')
    this.getOcxVersion()
  },
  methods: {
    downloadPlugin:function(localVersion,newVersion){
      var _this=this,
        msg="",
        localOcxVersion=''
      if(localVersion==-1){
        msg='控件没有安装或安装失败，请重新下载安装。'
      }else{
        localOcxVersion=localVersion.split(" ")[1]
        msg='当前版本 '+localOcxVersion+'<br>视频控件有更新，请重新下载安装。'
      }
      this.$confirm(msg, {
        closeOnClickModal:false,
        closeOnPressEscape:false,
        confirmButtonText: '下载',
        dangerouslyUseHTMLString:true,
        cancelButtonText: '稍后下载',
        title:'发现新版本视频控件 '+newVersion,
        type: 'warning'
      }).then(() => {
        location.href=_this.pluginUrl
      }).catch(() => {
      });
    },
    getOcxVersion:function(){
      var strJson={
        method:'getVersion',
        param:null
      },
        newVersion=this.BASE.newOcxVersion.replace(/\./g,'')
      try{
        var res=JSON.parse(this.ocxObj.PlayerCmd(JSON.stringify(strJson))),
          localVersion=(res.result.split(" ")[1]).replace(/\./g,'')
        console.log('107行',res);
        console.log(localVersion,newVersion);
        if(newVersion>localVersion){
          this.downloadPlugin(res.result,this.BASE.newOcxVersion)
        }
        sessionStorage.setItem('ocxVersion',res.result.split(" ")[1])
      }catch (e) {
        this.downloadPlugin(-1,this.BASE.newOcxVersion)
        sessionStorage.setItem('ocxVersion','控件没有安装或安装失败，请在登录界面重新下载安装。')
      }

    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      var _this=this
      // sessionStorage.setItem('userInfo',JSON.stringify({"username":"admin","userpassword":"888888888"}))
      // _this.$router.push('networkParameters')
      // return
      _this.$refs.loginForm.validate(function (valid) {
        if(valid){
          _this.loading = true
          _this.postAjax({
            method:'getuserinfo',
            params:{
              username:_this.loginForm.username,
              userpwd:_this.loginForm.password
            }
          }).then(function (res) {
            console.log(res);
            if(res.result==0){
              var data=res.params
              sessionStorage.setItem('userInfo',JSON.stringify(data))
              if(_this.loginForm.username==data.username&&_this.loginForm.password==data.userpassword){
                // console.log(_this);
                console.log(22222);
                sessionStorage.setItem('userInfo',JSON.stringify(data))
                _this.$router.push('networkParameters')
              }else{
                console.log('用户名或密码错误');
                _this.tips("用户名或密码错误","error");
                // _this.Message.tips('2222')
                // _this.Message.tips('用户名或密码错误','warning')
              }
            }else{
              _this.tips(res.errorInfo.detail,'error')
            }
          }).catch(function (err) {
            _this.tips('连接服务器失败','error')
            console.log(err);
          }).finally(function (obj) {
            console.log(obj);
            _this.loading = false
          })
        }else{
          return false
        }
      })


      return

      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || '/networkParameters' })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}
.pluginDownload:hover{
  text-decoration: underline;
}
/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  /*background-image: linear-gradient(to top, #09203f 0%, #537895 100%);*/
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    /*.logoTitle{*/
      /*!*margin:60px auto;*!*/
      /*font-size: 120px;*/
      /*text-align: center;*/
      /*font-weight: bold;*/
      /*background:-webkit-gradient(linear,30% 20%,80% 80%,from(#088df3),to(#6a38ec));*/
      /*-webkit-background-clip:text;*/
      /*-webkit-text-fill-color:transparent;*/
    /*}*/
    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
